<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
        @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row
      style="width: 100%;">
      <el-table-column label="ID" prop="id" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="账号" align="center">
        <template slot-scope="{row}">
          <span>{{ row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属公司" align="center">
        <template slot-scope="{row}">
          <span>{{ row.company }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" class-name="status-col">
        <template slot-scope="{row}">
          {{ row.create_time|formatDate}}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" class-name="status-col">
        <template slot-scope="{row}">
          {{ row.update_time|formatDate}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)" v-if="row.is_system==0">
            编辑
          </el-button>

          <el-button size="mini" type="danger" @click="deleteOperation(row.id)" v-if="row.is_system==0">
            删除
          </el-button>
          <el-button size="mini" type="danger" v-if="row.is_system==1">
            无操作
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <el-dialog :visible.sync="dialogFormVisible" :title="textMap[dialogStatus]">

      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
        style="width: 80%; margin-left:70px;">


        <el-form-item label="账号" prop="username">
          <el-input v-model="temp.username" type="text" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="所属公司" prop="c_id">
          <el-select v-model="temp.c_id" filterable placeholder="请选择">
            <el-option v-for="(item,index) in this.company_list" :key="index" :label="item.c_name" :value="item.id" />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="密码" prop="pass_word">
          <el-input v-model="temp.pass_word" type="text" placeholder="请输入密" />
        </el-form-item> -->
        <el-form-item label="姓名" prop="name">
          <el-input v-model="temp.name" type="text" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input v-model="temp.introduction" type="text" placeholder="请输入介绍" />
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="temp.role_id" placeholder="请选择">
            <el-option v-for="item in role_list" :key="item.id" :label="item.role_name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否股东账号" prop="is_shareholder">
          <el-select v-model="temp.is_shareholder" placeholder="请选择">
            <el-option  label="否" value="0" selected></el-option>
            <el-option  label="是" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择股东" prop="shareholder" v-if="temp.is_shareholder=='1'">
          <el-select v-model="temp.shareholder" placeholder="请选择">
            <el-option v-for="item in shareholderList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  var check_total_money = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('请再次输入密码'));
    } else if (value <= 0) {
      callback(new Error('必须大于0!'));
    } else {
      callback();
    }
  };
  import {
    create,
    update,
    getRoleList,
    deleteRecord,
    getAllList
  } from '@/api/user'
  import {getShareHolder} from '@/api/finance'
  import { merchantListOnlyMerchant } from '@/api/merchant'
  import {
    formatDate
  } from '@/utils/formatDate.js'
  import waves from '@/directive/waves' // waves directive
  import {
    parseTime
  } from '@/utils'
  import Pagination from '@/components/Pagination' // secondary package based on el-pagination
  export default {
    name: 'BalanceTable',
    components: {
      Pagination
    },
    directives: {
      waves
    },
    filters: {
      formatDate(time) {
        time = time * 1000
        let date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    data() {
      return {
        shareQuery:{
          limit:100
        },
        company_list:[],
        shareholderList:[],
        tableKey: 0,
        tableRecord: 1,
        list: [],
        total: 0,
        listLoading: true,
        dialogStatus: '',
        dialogFormVisible: false,
        role_list: [],
        listQuery: {
          page: 1,
          limit: 2000
        },
        temp: {
          is_shareholder:'0'
        },

        textMap: {
          update: 'Edit',
          create: 'Create'
        },
        rules: {
          username: [{
              required: true,
              message: '账号必须填写',
              trigger: 'blur'
            },
            {
              min: 6,
              max: 20,
              message: '长度在 6 到 20 个字符',
              trigger: 'blur'
            }
          ],
          c_id: [{
              required: true,
              message: '所属公司必选',
              trigger: 'blur'
            }
          ],
        
          role_id: [{
            required: true,
            message: '角色必须选择',
            trigger: 'change'
          }]

        }
      }
    },
    created() {
      this.listLoading = false
      this.getList();
      this.getCompanyList();
      
    },
    methods: {
      deleteOperation(id) {
        deleteRecord({id:id}).then(res => {
          this.$notify({
            title: 'Success',
            message: 'Created Successfully',
            type: 'success',
            duration: 2000,
            onClose: this.getList()
          })

        })
      },
      getShareHolder() {
        getShareHolder(this.shareQuery).then(response => {
          this.shareholderList = response.data.data;
        })
      },
      getCompanyList() {
        merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
          this.company_list = response.data.company
        })
      },
      handleCreate() {
        this.getShareHolder();
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.getRoleList()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      getRoleList() {
        getRoleList().then(resp => {
          this.role_list = resp.data
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            create(this.temp).then(() => {
              this.list.unshift(this.temp)
              this.dialogFormVisible = false
              this.$refs['dataForm'].resetFields();

              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000,
                onClose: this.getList()
              })
            })
          }
        })
      },
      handleUpdate(row) {
        this.getShareHolder();
        this.temp = Object.assign({}, row) // copy obj
        this.dialogStatus = 'update'
        this.temp.password = ''
        this.dialogFormVisible = true
        this.getRoleList()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)

            update(tempData).then(() => {
              this.$refs['dataForm'].resetFields();
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Update Successfully',
                type: 'success',
                duration: 2000,
                onClose: this.getList()
              })
            })
          }
        })
      },
      getList() {
        this.listLoading = true
        getAllList(this.listQuery).then(response => {
          this.list = response.data
          // this.total = response.data.count
          setTimeout(() => {
            this.listLoading = false
          }, 1.0 * 1000)
        })
      }
    }
  }
</script>

<style>
</style>
